package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Order;
import cn.wolfcode.crm.query.ProductQueryObejct;
import cn.wolfcode.crm.service.IOrderService;
import cn.wolfcode.crm.util.PageResult;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;

@Controller
@RequestMapping("order")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @RequestMapping("")
    public String index() {
        return "order";
    }

    @RequestMapping("list")
    @ResponseBody
    public PageResult list(ProductQueryObejct qo) {
        return orderService.query(qo);
    }


    //导出订单表格
    @RequestMapping("export")
    @ResponseBody
    public void export(ProductQueryObejct qo, HttpServletResponse response) throws Exception {

        //1.查询出列表数据
        qo.setRows(10000);
        List emps = orderService.queryForList(qo);

        response.setHeader("Content-Disposition", "attachment;filename=order.xls");
        OutputStream outputStream = response.getOutputStream();

        //创建一个工作簿,目标是 outputStream
        WritableWorkbook workbook = Workbook.createWorkbook(outputStream);

        //2.处理标题
        //创建一个可以写 sheet ,
        WritableSheet sheet = workbook.createSheet("order", 0);

        //创建一个单元格
        Label productNameLabel = new Label(0, 0, "商品名称");
        Label snLabel = new Label(1, 0, "单号");
        Label cosePriceLabel = new Label(2, 0, "商品单价");
        Label totalNumberLabel = new Label(3, 0, "数量");
        Label totalAmountLabel = new Label(4, 0, "销售金额");
        Label shopNameLabel = new Label(5, 0, "所属店铺");
        Label inputTimeLabel = new Label(6, 0, "消费时间");
        Label remarkLabel = new Label(7, 0, "备注");
        Label statusLabel = new Label(8, 0, "订单状态");


        sheet.addCell(productNameLabel);
        sheet.addCell(snLabel);
        sheet.addCell(cosePriceLabel);
        sheet.addCell(totalNumberLabel);
        sheet.addCell(totalAmountLabel);
        sheet.addCell(shopNameLabel);
        sheet.addCell(inputTimeLabel);
        sheet.addCell(remarkLabel);
        sheet.addCell(statusLabel);


        //3.循环迭代list,每一个order 对应表格中的一个行数据,二每一个order的每一个字段,应该对应一行的某一个单元格
        for (int i = 0; i < emps.size(); i++) {
            Order order = (Order) emps.get(i);

            int row = i + 1;

            Label tempproductNameLabel = new Label(0, row, order.getProduct().getProductName() == null ? "" : order.getProduct().getProductName());
            Label tempsnLabel = new Label(1, row, order.getOutcomeBill().getSn() == null ? "" : order.getOutcomeBill().getSn());
            Label tempcosePriceLabel = new Label(2, row, order.getProduct().getCosePrice().toString() == null ? "" : order.getProduct().getCosePrice().toString());
            Label temptotalNumberLabel = new Label(3, row, order.getOutcomeBill().getTotalNumber() == null ? "" : order.getOutcomeBill().getTotalNumber().toString());
            Label temptotalAmountLabel = new Label(4, row, order.getOutcomeBill().getTotalAmount() == null ? "" : order.getOutcomeBill().getTotalAmount().toString());
            Label tempshopNameLabel = new Label(5, row, order.getProduct().getShop().getName() == null ? "" : order.getProduct().getShop().getName());
            Label tempinputTimeLabel = new Label(6, row, order.getOutcomeBill().getInputTime() == null ? "" : order.getOutcomeBill().getInputTime().toLocaleString());
            Label tempremarkLabel = new Label(7, row, order.getRemark() == null ? "" : order.getRemark());

            String statusStr = "";
            if (order.getStatus() == 1) {
                statusStr = "待支付";
            } else if (order.getStatus() == 2) {
                statusStr = "已支付";
            } else {
                statusStr = "退款";
            }
            Label tempStatusLabel = new Label(8, row, statusStr);


            sheet.addCell(tempproductNameLabel);
            sheet.addCell(tempsnLabel);
            sheet.addCell(tempcosePriceLabel);
            sheet.addCell(temptotalNumberLabel);
            sheet.addCell(temptotalAmountLabel);
            sheet.addCell(tempshopNameLabel);
            sheet.addCell(tempinputTimeLabel);
            sheet.addCell(tempremarkLabel);
            sheet.addCell(tempStatusLabel);

        }
        //4.下文件
        //把内容中的工作簿对象的相关内容写到 输出流 里面
        workbook.write();
        //关闭工作簿
        workbook.close();
    }
}
